package com.shiku.im.user.dao.impl;

//import com.shiku.im.user.dao.ConfigDao;
import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.shiku.im.entity.Config;
import com.shiku.im.repository.MongoRepository;
import com.shiku.im.user.dao.ConfigDaoGet;
import com.shiku.im.user.dao.SignRecordDao;
import com.shiku.im.user.entity.SignRecord;
import com.shiku.im.utils.SKBeanUtils;
import org.bson.types.ObjectId;
import org.springframework.data.mongodb.core.FindAndModifyOptions;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Repository;

import java.util.Date;


/**
 * @author zhm
 * @version V1.0
 * @Description: TODO(todo)
 * @date 2019/9/3 12:30
 */
@Repository
public class SignRecordDaoImpl extends MongoRepository<SignRecord, ObjectId> implements SignRecordDao {

    @Override
    public Class<SignRecord> getEntityClass() {
        return SignRecord.class;
    }

    @Override
    public int getSignedCount(int userId){
        SignRecord record = queryOne("userId", userId);
        if(record == null){
            record = new SignRecord();
            record.setUserId(userId);
            record.setSignedCount(0);
            record.setLastSignTime(0);
            record.setId(new ObjectId());
            save(record);
            return 0;
        }
        Date lastSignDate = new Date(record.getLastSignTime());
        DateTime maySignDate = DateUtil.offsetDay(lastSignDate, 1);
        boolean in = DateUtil.isIn(new Date(), DateUtil.beginOfDay(maySignDate), DateUtil.endOfDay(maySignDate));
        if(in){
            return record.getSignedCount();
        }
        return 0;
    }

    @Override
    public int sign(int userId){
        SignRecord record = queryOne("userId", userId);
        if(record == null){
            record = new SignRecord();
            record.setUserId(userId);
            record.setSignedCount(1);
            record.setLastSignTime(System.currentTimeMillis());
            record.setId(new ObjectId());
            save(record);
            return 1;
        }
        Date lastSignDate = new Date(record.getLastSignTime());
        if(cn.hutool.core.date.DateUtil.between(new Date(record.getLastSignTime()), new Date(), DateUnit.DAY) == 0){
            return 0;
        }
        DateTime maySignDate = DateUtil.offsetDay(lastSignDate, 1);
        boolean in = DateUtil.isIn(new Date(), DateUtil.beginOfDay(maySignDate), DateUtil.endOfDay(maySignDate));
        if(in){
            record.setSignedCount(record.getSignedCount() + 1);
            record.setLastSignTime(System.currentTimeMillis());
            save(record);
            return record.getSignedCount();
        }
        record.setSignedCount(1);
        record.setLastSignTime(System.currentTimeMillis());
        save(record);
        return 1;
    }
}
